package com.example.datn.objects;

import com.example.datn.adapter.AlbumItemAdapter;
import com.example.datn.manager.UrlManager;
import com.example.datn.service.ImageService;

import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.util.Log;


public class AlbumObject {	
	public String albumId;
	public String albumName;
	public String albumDetail;
	public int singerId;
	public String linkImage;
	public String linkPlayAlbum;
	public int soLuotNghe;
	public Bitmap BitmapImage;
	public AlbumItemAdapter albumAdapter;
	
	public Bitmap getBitmapImage() {
		return BitmapImage;
	}

	public void setBitmapImage(Bitmap bitmapImage) {
		BitmapImage = bitmapImage;
	}

	public AlbumItemAdapter getAlbumAdapter() {
		return albumAdapter;
	}

	public void setAlbumAdapter(AlbumItemAdapter albumAdapter) {
		this.albumAdapter = albumAdapter;
	}

	public void loadImage(AlbumItemAdapter adapter) {
		this.albumAdapter = adapter;
		if (linkImage != null || "".equals(linkImage)) {
			new ImageLoadTask().execute(this.linkImage);
		}
	}

	public void release(){
		this.BitmapImage.recycle();
	}
	
	private class ImageLoadTask extends AsyncTask<String, String, Bitmap> {

		@Override
		protected void onPreExecute() {
			Log.i("ImageLoadTask", "Loading image...");
		}

		// PARAM[0] IS IMG URL
		@Override
		protected Bitmap doInBackground(String... param) {
			//String url = UrlManager.rootIp+param[0];
			String url = param[0];
			Log.i("ImageLoadTask", "Attempting to load image URL: " + url);
			try {				
				Bitmap b = ImageService.getBitmapFromURL(url);
				return b;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}

		@Override
		protected void onProgressUpdate(String... progress) {
			// NO OP
		}

		@Override
		protected void onPostExecute(Bitmap ret) {
			if (ret != null) {
				Log.i("ImageLoadTask", "Successfully loaded " + albumName
						+ " image");
				BitmapImage = ret;
				if (albumAdapter != null) {
					// WHEN IMAGE IS LOADED NOTIFY THE ADAPTER
					albumAdapter.notifyDataSetChanged();
				}
			} else {
				Log.e("ImageLoadTask", "Failed to load " + albumName + " image");
			}
		}
	}

}
